//
//  ViewController.m
//  UI_省市区
//
//  Created by 才诗琦 on 15/12/22.
//  Copyright © 2015年 www.lanou3g.com. All rights reserved.
//

#import "ViewController.h"
#import "NSString+Characters.h"
#import "CitiesViewController.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataArr;
@property(nonatomic, retain)NSMutableDictionary *pDic;
@property(nonatomic, retain)NSMutableDictionary *cDic;
@property(nonatomic, retain)NSMutableArray *tempA;



@property(nonatomic, retain)NSMutableDictionary *shDic;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    
    [self.view addSubview:_tableView];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    
    [_tableView release];
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tempA = [NSMutableArray array];
    //数据处理
    [self data];
}

- (void)data
{
    
    NSString *areaText = [NSString stringWithContentsOfFile:@"/Users/dllo/Desktop/DLS151129-41/UI/UI_省市区/UI_省市区/area.txt" encoding:NSUTF8StringEncoding error:nil];
    
   NSArray *arr = [areaText componentsSeparatedByString:@"\n"];
    NSMutableArray *pArr = [NSMutableArray array];
    
//    NSMutableArray *cArr = [NSMutableArray array];
//    
//    NSMutableArray *qArr = [NSMutableArray array];
    for (NSString *temp in arr ) {
        if (![temp hasPrefix:@" "]) {
            self.pDic = [NSMutableDictionary dictionary];
            
            [_pDic setObject:temp forKey:@"PName"];
            [_pDic setObject:[NSMutableArray array] forKey:@"cities"];
            [pArr addObject:_pDic];

       }
  
        if ([temp hasPrefix:@"  "] &&![temp hasPrefix:@"    "]) {
            self.cDic = [NSMutableDictionary dictionary];
            [_cDic setObject:temp forKey:@"CName"];
            [_cDic setObject:[NSMutableArray array] forKey:@"zones"];
            NSMutableDictionary *pDic = pArr.lastObject;
          NSMutableArray *cArr = [pDic objectForKey:@"cities"];
            [cArr addObject:_cDic];
        }
    
        if ([temp hasPrefix:@"    "]) {
            NSMutableDictionary *pDic = pArr.lastObject;
            NSMutableArray *cArr = [pDic objectForKey: @"cities"];
            NSMutableDictionary *cDic = cArr.lastObject;
            NSMutableArray *qArr = [cDic objectForKey:@"zones"];
            [qArr addObject:temp];
            
        }
    }
    
    
    //遍历数组
    for (NSInteger i = 0;i < pArr.count;i ++) {
               self.tempA[i] = pArr[i];
        
    }
    
// self.tempA = pArr ;
    
       self.shDic = [NSMutableDictionary dictionary];
    
    for (NSDictionary *dic in pArr) {
       
            NSString *captialLetter = [dic[@"PName"] firstCharacterOfName];
                NSMutableArray *Arr = _shDic[captialLetter];
                                
                if (Arr) {
                    [Arr addObject:dic];
                }
                else{
                    Arr = [NSMutableArray array];
                    [Arr addObject:dic];
                    [_shDic setObject:Arr forKey:captialLetter];
                }
                
                                       
            } ;
 
            self.dataArr = [[_shDic.allKeys sortedArrayUsingSelector:@selector(compare:)] mutableCopy];
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return  _shDic.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = _dataArr[section];
    
    NSArray *arr = _shDic[key];
    return arr.count;
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return _dataArr[section];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    NSString *key = _dataArr[indexPath.section];
    NSArray *arr = _shDic[key];
    NSDictionary *dic = arr[indexPath.row];
    
    
    
    cell.textLabel.text = dic[@"PName"];
    
    return cell;
    
    
}

//点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    CitiesViewController *citVC = [[CitiesViewController alloc] init];
    
    
    NSString *key = _dataArr[indexPath.section];
    NSArray *arr = _shDic[key];
    NSDictionary *dic = arr[indexPath.row];
       citVC.dit = dic;
   
    citVC.karr = _tempA;
    
[self.navigationController pushViewController:citVC animated:YES];
    
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
